
import TIM from '../../TUIKit/lib/tim-wx-sdk';
import TIMUploadPlugin from '../../TUIKit/lib/tim-upload-plugin';
import { genTestUserSig } from '../../TUIKit/debug/GenerateTestUserSig.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
      inputvalue:'',
      friend_id:0,
      chatlist:[],
      IM:{
        SDKAppID:"1400759366"
       }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function(options) {
      console.log(options)
      let that = this
      let token = wx.getStorageSync('token')
      that.setData({
        friend_id:options.id
      })
      this.getUserIM(options.id,token)
      wx.request({
        url: 'http://www.house.com/index.php/api/searchfriend',
        header:{
          token:token
        },
        data:{
          friend:options.id
        },
        success:res=>{
          console.log(res)
          if(res.data.code==200){
              this.setData({
                chatlist:res.data.data
              })
          }
        }
      })
  },
  getInputValue(e){
      console.log(e)
      let that = this
      that.setData({
        inputvalue:e.detail.value
      })
  },
  publishChat(){
   
      let friend_id = this.data.friend_id
      let inputvalue = this.data.inputvalue
      let type = "TIMTextElem"
      let token = wx.getStorageSync('token')
      let chatlist = this.data.chatlist
      wx.request({
        url: 'http://www.house.com/index.php/api/sendfriend',
        header:{
          token:token
        },
        data:{
          receive:friend_id,
          content:inputvalue,
          type:type
        },
        success:res=>{
          console.log(res.data.data)
          let newchatlist = res.data.data
          let friendchatlist = chatlist.concat(newchatlist)
          if(res.data.code==200){
              this.setData({
                chatlist:friendchatlist
              })
          }
        }
      })
  },
  getUserIM(friend,token){

     wx.request({
        url: 'http://www.house.com/index.php/api/userim',
        header:{
          token:token
        },
        data:{
          friend:friend
        },
        success:res=>{
          console.log(res.data)
          if(res.data.code==200){
            this.monitorMessage(res.data.data.myIM,res.data.data.mysig,res.data.data.friendIM)
          }
        }
      })
  },
  async monitorMessage(userID,userSig,friendIM){
        let sdkid = this.data.IM
        let tim = TIM.create(sdkid)
        let friend = this.data.friend_id
        let chatlist = this.data.chatlist
        let that = this
        tim.registerPlugin({'tim-upload-plugin': TIMUploadPlugin});
    
        let onMessageReceived = function(event) {
          console.log(event)
          
          if(event.data[0].from==friendIM){
             let data = {
               id:friend,
               nickname:event.data[0].nick,
               user_image:event.data[0].avatar,
               content:{
                 Text:event.data[0].payload.text
               }
             }
             let newchatlist = chatlist.concat(data)
              that.setData({
                  chatlist:newchatlist
              })
          }
        };
      tim.on(TIM.EVENT.MESSAGE_RECEIVED, onMessageReceived);
      const promise = await tim.login({
        userID: userID,
        userSig:userSig 
      });
      if (!promise.code) {
        console.log('IM登陆成功', promise);
      }
  }
})